﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 
using System.Text;
using Common;

public partial class website_admin_knowlegebase_modeleditor : PageBase
{
    string TitleImage = ConfigurationManager.AppSettings["TitleImage"].ToString();
    string AuthenticateImages = ConfigurationManager.AppSettings["AuthenticateImages"].ToString();

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        /// 保存设置
        if (RequestBase.GetFormString("___EVENTTARGET") == "save")
        {
            Save();
        }

        if (!IsPostBack)
        {
            if (RequestBase.GetQueryString("articleid") != "")
                Initialize();
        }
    }
 
    #region 数据初始化
    void Initialize()
    {
        Model.KbArticleBase _obj = BLL.KbArticleBase.GetObjById(RequestBase.GetQueryString("articleid"));
        ArticleObj = _obj;
        AttachmentsObj = BLL.AttachmentsBase.GetAttachments(RequestBase.GetQueryString("articleid"));
    }

    Model.KbArticleBase ArticleObj
    {
        get
        {
            Model.KbArticleBase _article = new Model.KbArticleBase();

            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("articleid")))
                _article.ArticleId = RequestBase.GetQueryString("articleid");

            _article.Title = txtTitle.Value.Trim();

            if (txtArticleNumber.Value.Trim() == "AUTO")
            {
                _article.ArticleNumber = Decimal.Parse(BLL.KbArticleBase.GetArticleNumber(RequestBase.GetQueryString("c_number")));
            }
            else
                _article.ArticleNumber = Decimal.Parse(string.IsNullOrEmpty(txtArticleNumber.Value.Trim()) && Utils.IsNumber(txtArticleNumber.Value) ? "0" : txtArticleNumber.Value.Trim());

            _article.CategoryId = BLL.KbCategoryBase.GetCategoryIdByNumber(RequestBase.GetQueryString("c_number"));
            _article.Resume = txtResume.Value;

            _article.PublishDate = DateTime.Now;
            _article.ModifiedOn = DateTime.Now;
            _article.CreatedOn = DateTime.Now;
            _article.ModifiedBy = this.SystemUserId;
            _article.CreatedBy = this.SystemUserId;
            _article.TitlePic = txtUploadControl_TitleImage.Value.Trim().Replace("|", "");

            return _article;
        }
        set
        {
            if (value != null)
            {
                txtTitle.Value = value.Title.Trim();
                txtArticleNumber.Value = value.ArticleNumber.ToString();
                txtResume.Value = value.Resume;

                if (value.TitlePic != "")
                {
                    txtUploadControl_TitleImage.Value = "|" + value.TitlePic.Trim();
                    divTitleImageList_TitleImage.InnerHtml = "<div class=\"attachmentitems\">" + BLL.AttachmentsBase.GetIcoByExtName(value.TitlePic.Trim().Split('*')[2]) + "&nbsp;&nbsp;<a href='../../" + TitleImage + value.TitlePic.Trim().Split('*')[1] + "' title='单击打开文件' target='_blank'>" + value.TitlePic.Trim().Split('*')[0] + "</a>&nbsp;&nbsp; <a href='javascript:void(0);' filedata='" + value.TitlePic.Trim() + "' itemid='TitleImage' onclick='uploadinit($(this));'>[删除]</a></div>";
                } 
            }
        }
    }


    Model.AttachmentsBase[] AttachmentsObj
    {
        get
        {
            if (txtUploadControl_AuthenticateImages.Value.Trim() != "")
            {
                string fileData = "#" + txtUploadControl_AuthenticateImages.Value;
                fileData = fileData.Replace("#|", "");
                string[] arrItems = fileData.Split('|');

                Model.AttachmentsBase[] fileItems = new Model.AttachmentsBase[arrItems.Length];
                int i = 0;
                foreach (string item in arrItems)
                {
                    fileItems[i] = new Model.AttachmentsBase("00000000-0000-0000-0000-000000000000", item.Split('*')[0], item.Split('*')[1], item.Split('*')[2], int.Parse(item.Split('*')[3]), decimal.Parse(item.Split('*')[4]), RequestBase.GetFormString("txtAttachmentFileNotes_" + item.Split('*')[1]), (i + 1).ToString());
                    i++;
                }
                return fileItems;
            }
            else
                return null;
        }
        set
        {
            if (value != null)
            {
                StringBuilder attachments = new StringBuilder();
                string _filedata = "";
                foreach (Model.AttachmentsBase _item in value)
                {
                    _filedata = _item.FileName + "*" + _item.FilePath + "*" + _item.ExtName + "*" + _item.FileType.ToString() + "*" + _item.FileSize.ToString();
                    attachments.Append("<div class=\"attachmentitems\">");
                    attachments.Append(BLL.AttachmentsBase.GetIcoByExtName(_item.ExtName.Trim()));
                    attachments.Append("&nbsp;<a href='../../" + AuthenticateImages + _item.FilePath.Trim());
                    attachments.Append("' title='单击打开文件' target='_blank'>" + _item.FileName.Trim());
                    attachments.Append("</a><br />注释：&nbsp;<input type=\"text\" style=\"width: 320px;\" name=\"txtAttachmentFileNotes_");
                    attachments.Append(_item.FilePath.Trim() + "\" value=\"" + _item.Notes.Trim() + "\" />&nbsp;&nbsp;<a href='javascript:void(0);' filedata='");
                    attachments.Append(_filedata + "' itemid='AuthenticateImages' onclick='uploadinit($(this));'>[删除]</a></div>");
                    txtUploadControl_AuthenticateImages.Value += "|" + _filedata;
                }

                divAuthenticateImagesList_AuthenticateImages.InnerHtml = attachments.ToString();
            }
        }
    }
    #endregion

    #region 保存数据
    void Save()
    {
        string res = "";

        if (RequestBase.GetQueryString("otype").Trim() == "add")
        {
            res = BLL.KbArticleBase.Add(ArticleObj, AttachmentsObj);
        }

        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            res = BLL.KbArticleBase.Update(ArticleObj, AttachmentsObj);
        }

        switch (res.Split('|')[0])
        {
            case "succeeded":
                JScript.AlertAndReturnValueToParent("信息保存成功!", "window.opener.LoadingData(); window.close();");
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }
    #endregion
}
